package com.pj.wisdomlogistics;

import android.content.Context;
import android.os.Environment;
import android.support.multidex.MultiDex;
import android.support.multidex.MultiDexApplication;
import android.text.TextUtils;

import com.facebook.stetho.Stetho;
import com.facebook.stetho.okhttp3.StethoInterceptor;
import com.pj.wisdomlogistics.api.LoggerInterceptor;
import com.pj.wisdomlogistics.cons.Cons;
import com.pj.wisdomlogistics.entity.User;
import com.pj.wisdomlogistics.utils.CrashHandler;
import com.pj.wisdomlogistics.utils.NetworkUtils;
import com.pj.wisdomlogistics.utils.SPUtils;
import com.squareup.leakcanary.LeakCanary;
import com.squareup.leakcanary.RefWatcher;
import com.zhy.http.okhttp.OkHttpUtils;

import java.io.File;
import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;




public class MyApplication extends MultiDexApplication {
    private static MyApplication mInstance;
    public static boolean isGzh = false;
    private RefWatcher refWatcher;

    public static String userID;
    public static String token = "";
    public static User user;
    public static int userType;

    public static boolean isNetOk;

    public static boolean isJc = false; //是否是集成在智慧新城移动平台

    public static String signPath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "sign.png";

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }
    @Override
    public void onCreate() {
        super.onCreate();

        isNetOk = NetworkUtils.isConnected(getApplicationContext());

        OkHttpClient client = new OkHttpClient.Builder()
                .addNetworkInterceptor(new StethoInterceptor())
                .addInterceptor(new LoggerInterceptor("TAG"))
                .connectTimeout(10000L, TimeUnit.MILLISECONDS)
                .readTimeout(10000L, TimeUnit.MILLISECONDS)
                //其他配置
                .build();

        OkHttpUtils.initClient(client);

        mInstance = this;

        initSpUtils();
        performInit();

    }

    private void performInit() {
        if (BuildConfig.DEBUG) {
            //initLeakCanary();
            initStetho();

        }
        else {
            initCrashHandler();
        }
    }


    private void initLeakCanary() {
        refWatcher = LeakCanary.install(this.getApplication());
    }

    private void initStetho() {
        Stetho.initialize(Stetho.newInitializerBuilder(this.getApplicationContext())
                .enableDumpapp(Stetho.defaultDumperPluginsProvider(this.getApplicationContext()))
                .enableWebKitInspector(Stetho.defaultInspectorModulesProvider(this.getApplicationContext()))
                .build());
    }

    private void initCrashHandler() {
        CrashHandler.getInstance().init(this.getApplicationContext());
    }

    public static MyApplication  getApplication(){
        return mInstance;
    }

    private void initSpUtils() {
        SPUtils.init(this, Cons.SPCons.SP_NAME);
    }
    public static void loadUser(){
        boolean flag = false;
        user = SPUtils.loadUser();
        if (user != null) {
            userID = user.getId();
            token = user.getAccessToken();
            userType = user.getType();
            for (int i = 0; i < user.getRoleId().size(); i++) {
                if (TextUtils.equals(user.getRoleId().get(i) , Cons.RoleId.GZR)){
                    flag = true;
                    isGzh = flag;
                    return;
                }
            }
        }
        isGzh = flag;
    }

    public static void logout(){
        SPUtils.saveUser(null);
        userID = "";
        token = "";
        user = null;
    }

}
